<?php

class Groups_Model extends Model 
{
	function Groups_Model()
	{
		parent::Model();
		
		$this->load->config('redux_auth');
		$this->_tables  = $this->config->item('tables');
	}
	
	function get_all()
	{
		$this->db->order_by('id', 'asc');
		return $this->db->get($this->_tables['groups'])->result();
	}
	
	function get_by_id($id)
	{
		$this->db->where('id', $id);
		return $this->db->get($this->_tables['groups'])->row();
	}
	
	function get_by_name($name)
	{
		$this->db->where('name', $name);
		return $this->db->get($this->_tables['groups'])->row();
	}
	
	function create($name, $description = "")
	{
		$data = array(
			'name' => $name,
			'description' => $description
		);
            
		$this->db->insert($this->_tables['groups'], $data);
	}
	
	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete($this->_tables['groups']);	

		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	function update($field_data = array()){
		
		if(isset($field_data['id'])){			
			$group = $this->get_by_id($field_data['id']);
			
			if($group != NULL){
				$defaults = array(
					'name'			=> $group->name,
					'description'	=> $group->description
				);
				
				$field_data = array_merge($defaults, $field_data);
				
				$data = array (
					'name'			=> $field_data['name'],
					'description'	=> $field_data['description']
				);
				
				$this->db->where('id', $field_data['id']);
				
				$this->db->update($this->_tables['groups'], $data);
										
				return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
			}
		}
		
		return FALSE;
	}
}

?>